import configparser
import os
from commond import get_path_info


class ReadConfig(object):
    def __init__(self, config_name, test_project_name):
        """
        :param config_name: 配置文件名称
        :param test_project_name: 测试项目名称
        """
        config_path = os.path.join(get_path_info.get_test_project(test_project_name), "testfile",
                                   config_name)  # 获取配置文件路径
        self.config = configparser.ConfigParser()  # 调用外部的读取配置文件的方法
        self.config.read(config_path, encoding="utf-8")

    def get_http(self, name, key):
        value = self.config.get(name, key)
        return value

    def get_email(self, name, key):
        value = self.config.get(name, key)
        return value

    def get_mysql(self, name, key):  # 数据库相关，
        value = self.config.get(name, key)
        return value

    def get_project_info(self, name, key):
        value = self.config.get(name, key)
        return value

    def get_url(self):
        new_url = self.get_http("HTTP", 'scheme') + '://' + self.get_http("HTTP",
                                                                          'base_url') + ':' + self.get_http("HTTP",
                                                                                                            "port")
        return new_url


if __name__ == '__main__':
    print('HTTP中的baseurl值为：', ReadConfig("config.ini", "oa_case").get_http("HTTP", 'base_url'))
    print('EMAIL中的开关on_off值为：', ReadConfig("config.ini", "oa_case").get_email("EMAIL", 'on_off'))
    print('Report_name：', ReadConfig("config.ini", "oa_case").get_project_info("PROJECT", 'report_name'))
    print('URL：', ReadConfig("config.ini", "oa_case").get_url())
